package com.cdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdu.annotation.OperationLogAnnotation;
import com.cdu.enums.MessageEnum;
import com.cdu.enums.OperationLogEnum;
import com.cdu.mapper.NewsMapper;
import com.cdu.model.common.BaseResponse;
import com.cdu.model.common.PageResult;
import com.cdu.model.query.NewsQuery;
import com.cdu.pojo.News;
import com.cdu.service.NewsService;
import com.cdu.util.ParamUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * projectName : easy-help-v2
 *
 * @author :lql
 * date: 2022 -05 -19
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private final NewsMapper newsMapper;

    @Override
    public BaseResponse queryAllNews(NewsQuery newsQuery) {
        PageHelper.startPage(newsQuery.getPageNum(),newsQuery.getPageSize());
        LambdaQueryWrapper<News> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(newsQuery.getNewsTitle()),News::getNewsTitle, newsQuery.getNewsTitle());
        List<News> newsList = newsMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(newsList);
        return BaseResponse.response(PageResult.doPage(pageInfo.getList(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getTotal()), MessageEnum.SUCCESS);
    }

    @Override
    public BaseResponse createNews(JSONObject ido) {
        News news = new News();
        String title = ido.getString("newsTitle");
        String content = ido.getString("content");
        String creator = ido.getString("creator");
        news.setNewsTitle(title);
        news.setContent(content);
        news.setViews(0);
        news.setCreator(creator);
        news.setCreateUserId(creator);
        news.setCreateTime(new Date());
        newsMapper.insert(news);
        return BaseResponse.response(MessageEnum.SUCCESS);
    }


    @Override
    public BaseResponse queryNewById(NewsQuery newsQuery) {
        News news = newsMapper.selectById(newsQuery.getId());
        return  BaseResponse.response(news,MessageEnum.SUCCESS);
    }

    @Override
    public BaseResponse updateNews(JSONObject ido) {
        String id = ido.getString("id");
        News news = newsMapper.selectById(id);
        Integer views = Integer.valueOf(ido.getString("views"));
        news.setViews(views);
        news.setUpdateTime(new Date());
        newsMapper.updateById(news);
        return BaseResponse.response(MessageEnum.SUCCESS);
    }

    @Override
    public BaseResponse delNewsById(NewsQuery newsQuery) {
        Integer isdel = newsMapper.deleteById(newsQuery.getId());
        if (isdel==1){
            return  BaseResponse.response(MessageEnum.SUCCESS);
        }
        return BaseResponse.response(MessageEnum.ERROR);
    }

    @Override
    public BaseResponse detailNews(NewsQuery newsQuery) {
        News news = newsMapper.selectById(newsQuery.getId());
        news.setViews(news.getViews()+1);
        newsMapper.updateById(news);
        return  BaseResponse.response(news,MessageEnum.SUCCESS);
    }
}
